using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;  
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 药品管理系统
{
    public partial class frm_Medcine : Form
    {
        public frm_Medcine()
        {
            InitializeComponent();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                          //声明并实例化SQL连接；
            sqlConnection.ConnectionString =
                "Server=KK的秘密;Database=MMS;Integrated Security=sspi";                        //在字符串变量中，描述连接字符串所需的服务器地址、数据库名称、集成安全性（即是否使用Windows验证）；
            SqlCommand sqlCommand = sqlConnection.CreateCommand();                                      //调用SQL连接的方法CreateCommand来创建SQL命令；该命令将绑定SQL连接；
            sqlCommand.CommandText =
                "INSERT tb_medicine (Mno,Mname,Mtype,RegisteredNo,Mrecipe,Mprice,Mdate,Sno) VALUES(@Mno,@Mname,@Mtype,@RegisteredNo,@Mrecipe,@Mprice,@Mdate,@Sno);";    //指定SQL命令的命令文本；命令文本包含参数；
            sqlCommand.Parameters.AddWithValue("@Mno", this.txb_MedicineNo.Text.Trim());                     //向SQL命令的参数集合添加参数的名称、值；
            sqlCommand.Parameters.AddWithValue("@Mname", this.txb_MedicineName.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Mtype", this.cmb_Type.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@RegisteredNo", this.txb_RegisteredNo.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Mrecipe", this.richTextBox1.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Mprice", this.txb_Mprice.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Mdate", this.dtp_Mdate.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Sno", this.cmb_Sno.Text.Trim());                        //将密码参数的类型设为变长字符串；
            //SQL参数自动识别类型；若参数值为字符串，则类型自动设为NVARCHAR，且可在执行时自动转换；但对于相同密码，VARCHAR/NVARCHAR类型所获得的散列值不同，故需手动将SQL参数类型统一设为VARCHAR;
            sqlConnection.Open();                                                                       //打开SQL连接；
            int rowAffected = sqlCommand.ExecuteNonQuery();                                             //调用SQL命令的方法ExecuteNonQuery来执行命令，向数据库写入数据，并返回受影响行数；
            sqlConnection.Close();
            if (rowAffected == 1)                                                                       //若成功写入1行记录；
            {
                MessageBox.Show("添加记录成功");                                                          //给出正确提示；
            }
            else                                                                                        //否则；
            {
                MessageBox.Show("添加记录失败");
            }
        }

        private void frm_Medcine_Load(object sender, EventArgs e)
        {
          
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
             "Server=KK的秘密;Database=MMS;Integrated Security=sspi"; 
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_medicine WHERE Mno=@No;";
            sqlCommand.Parameters.AddWithValue("@No", txb_MedicineNo.Text);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.Read())
            {
                this.txb_MedicineName.Text = sqlDataReader["Mname"].ToString();
                this.cmb_Type.Text = sqlDataReader["Mtype"].ToString();
                this.txb_RegisteredNo.Text = sqlDataReader["RegisteredNo"].ToString();
                this.richTextBox1.Text = sqlDataReader["Mrecipe"].ToString();
                this.txb_Mprice.Text = sqlDataReader["Mprice"].ToString();
                this.dtp_Mdate.Text = sqlDataReader["Mdate"].ToString();
                this.cmb_Sno.Text = sqlDataReader["Sno"].ToString();
            }
            sqlDataReader.Close();
          
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();                                          //声明并实例化SQL连接；
            sqlConnection.ConnectionString =
                "Server=KK的秘密;Database=MMS;Integrated Security=sspi";                      //在字符串变量中，描述连接字符串所需的服务器地址、数据库名称、集成安全性（即是否使用Windows验证）；
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText =
                "delete from tb_medicine where Mno='" + txb_MedicineNo.Text + "'";
            sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            sqlConnection.Close();
            if (rowAffected == 1)                                                                       //若成功写入1行记录；
            {
                MessageBox.Show("删除成功！");                                                          //给出正确提示；
            }
            else                                                                                        //否则；
            {
                MessageBox.Show("请检查输入编号是否正确！");                                                          //给出错误提示；
            }
        }

        private void cmb_Sno_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void 菜单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Menu a = new frm_Menu();
            a.Show();
            this.Hide();
        }

        private void 登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Login b = new frm_Login();
            b.Show();
            this.Hide();
        }

        private void 注册ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Register c = new frm_Register();
            c.Show();
            this.Hide();
        }

    }
}
